<?php
class Commqrank extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 10;
        $this->_form_type = 'comm_answer_audio';
        $this->_list_where = array();
        $this->_action_list = array(array('controller'=>'admin/commqrank/','function'=>'addanswer','label'=>'回答'));//EDIT,DEL,array('controller'=>'admin/commanswer/','function'=>'showlist','label'=>'查看回答')
        $this->_controller = 'admin/commqrank/';//admin/user/
        parent::__construct();
        $this->load->model('adminqrank_model');
    }

    public function showList($page = 1)
    {
        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $res = $this->getList($offset,$limit);
        $pdata = $this->getPagination($this->_controller.'showList/',$res['total']);
        $switchdata = array(
            array('href'=>site_url($this->_controller.'showList/'),'label'=>'未回答','active'=>1),
            array('href'=>site_url($this->_controller.'getResponsed/'),'label'=>'已回答','active'=>0)
        );
        $switch = $this->load->view('admin/form/other/commqrankswitch',array('switches'=>$switchdata),true);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'other'=>$switch);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }

    public function getResponsed($page=1)
    {
        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $res = $this->getResponsedList($offset,$limit);

        $pdata = $this->getPagination($this->_controller.'getResponsed/',$res['total']);
        $switchdata = array(
            array('href'=>site_url($this->_controller.'showList/'),'label'=>'未回答','active'=>0),
            array('href'=>site_url($this->_controller.'getResponsed/'),'label'=>'已回答','active'=>1)
        );
        $switch = $this->load->view('admin/form/other/commqrankswitch',array('switches'=>$switchdata),true);
        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'other'=>$switch);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }
    //重写获取list数据
    public function getList($offset = 0,$limit = 0)
    {
        $res = $this->adminqrank_model->getlist($offset,$limit);
        return $res;
    }
    public function getResponsedList($offset = 0,$limit = 0)
    {
        $res = $this->adminqrank_model->getResponsedList($offset,$limit);
        return $res;
    }
    public function addanswer($qid = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $dbdata=array();
            $dbdata['created'] = time();
            $dbdata['changed'] = time();
            $dbdata['qid'] = (int)$qid;
            $dbdata['title'] = $post['title'];
            $voices = $this->uploadvoice('audio');
            if ($voices['code'] == 1){
                $dbdata['audio'] = implode("|",$voices['voiceurl']);
            }
            $imgs = $this->uploadImg('note',500);
            if ($imgs['code'] == 1){
                $dbdata['note'] = implode("|",$imgs['imgurl']);
            }
            $this->_create_data = $dbdata;
            parent::create();
        }
    }
    public function aftercreate($pkeyval)
    {
        return $this->adminqrank_model->setQResponsed($pkeyval);
    }

    public function updateanswer($id = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            $dbdata=array();
            $dbdata['changed'] = time();
            $dbdata['title'] = $post['title'];
            $voices = $this->uploadvoice('audio');
            if ($voices['code'] == 1){
                $dbdata['audio'] = implode("|",$voices['voiceurl']);
            }
            $oldimgs = $this->adminform_model->getRowData('comm_answer_audio','note',array('id'=>$id));
            $oldimgs = $oldimgs['note'];
            $delimgs = $post['del'];
            if (!empty($delimgs)||$delimgs==='0'){
                $delimgs=explode('|',$delimgs);
                $oldimgs = explode('|',$oldimgs);
                foreach ($delimgs as $d){
                    unset($oldimgs[$d]);
                }
                $oldimgs = implode('|',$oldimgs);
            }
            $dbdata['note'] = $oldimgs;
            $imgs = $this->uploadImg('note',500);
            if ($imgs['code'] == 1){
                if (!empty($dbdata['note'])) $dbdata['note'] .= '|'.implode("|",$imgs['imgurl']);
                else $dbdata['note'] = implode("|",$imgs['imgurl']);
            }
            $this->_update_data = $dbdata;
            parent::update($id);
        }

    }

    public function getPagination($uri = '',$total = 0)
    {
        $this->load->library('pagination');
        $config['base_url'] = site_url($uri);
        $config['total_rows'] = $total;
        $config['per_page'] = $this->_page_size;
        $config['cur_tag_open'] = '<div class="cur-page">';
        $config['cur_tag_close'] = '</div>';
        $config['use_page_numbers'] = TRUE;
        $config['num_tag_open'] = '<div class="pageitem">';
        $config['num_tag_close'] = '</div>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<div class="pre-page">';
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<div class="next-page">';
        $config['next_tag_close'] = '</div>';
        $config['last_link'] = '最后一页';
        $config['last_tag_open'] = '<div class="last-page">';
        $config['last_tag_close'] = '</div>';
        $config['first_link'] = '第一页';
        $config['first_tag_open'] = '<div class="first-page">';
        $config['first_tag_close'] = '</div>';
        $config['full_tag_open'] = '<div class="page-total-results">共'.$total.'条，每页'.$this->_page_size.'条</div>';

        $this->pagination->initialize($config);

        return array('total'=>$total,'pagination'=>$this->pagination->create_links());
    }

}